//
//  NumeberTileGameViewController.swift
//  StoreMaker
//
//  Created by 展恒 on 15/12/30.
//  Copyright © 2015年 赵百川. All rights reserved.
//

import UIKit

class NumeberTileGameViewController: UIViewController {
    
    var dimension : Int = 0
    var threshold : Int = 0
    
    var board:GameboardView?
    var model:GameModel?
    
    var scoreView:ScoreViewProtocol?
    
    let boardWidth: CGFloat = UIScreen.mainScreen().bounds.width
    
    let thinPadding: CGFloat = 3.0
    let thickPadding: CGFloat = 6.0
    
    let viewPadding:CGFloat = 10.0
    
    let verticalViewOffset:CGFloat = 0.0
    
    init(dimension d:Int,threshold t:Int)
    {
        dimension = d > 2 ? d : 2
        threshold = t > 8 ? t : 8
        super.init(nibName: nil, bundle: nil)
//        model = GameModel(dimension: dimension, threshold: threshold, delegate: self)
        view.backgroundColor = UIColor.whiteColor()
        setupSwipeControls()
    }
    func setupSwipeControls(){
        let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("up:"))
        upSwipe.numberOfTouchesRequired = 1
        upSwipe.direction = UISwipeGestureRecognizerDirection.Up
        view.addGestureRecognizer(upSwipe)
        
        let downSwip = UISwipeGestureRecognizer(target: self, action: Selector("down:"))
        downSwip.numberOfTouchesRequired = 1
        downSwip.direction = UISwipeGestureRecognizerDirection.Down
        view.addGestureRecognizer(downSwip)
        
        let leftSwip = UISwipeGestureRecognizer(target: self, action: Selector("left:"))
        leftSwip.numberOfTouchesRequired = 1
        leftSwip.direction = UISwipeGestureRecognizerDirection.Left
        view.addGestureRecognizer(leftSwip)
        
        let rightSwip = UISwipeGestureRecognizer(target: self, action: Selector("right:"))
        rightSwip.numberOfTouchesRequired = 1
        rightSwip.direction = UISwipeGestureRecognizerDirection.Right
        view.addGestureRecognizer(rightSwip)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
    }
    func reset(){
        assert(board != nil && model != nil)
        let b = board!
        let m = model!
        b.reset()
        m.reset()
        m.insertTileAtRandomLocation(2)
        m.insertTileAtRandomLocation(2)
        
    }
    func setupGame(){
        let vcHeight = view.bounds.size.height
        let vcWidth = view.bounds.size.width
        //这个嵌套函数提供了一个组件视图的坐标
        func xPositionToCenterView(v:UIView) -> CGFloat{
            let viewWidth = v.bounds.size.width
            let tentativeX = 0.5 * (vcWidth - viewWidth)
            return tentativeX >= 0 ? tentativeX : 0
        }
        //这个嵌套函数提供了一个组件视图的坐标
        func yPositionForViewAtPosition(order:Int,views:[UIView]) -> CGFloat{
            assert(views.count > 0)
            assert(order >= 0 && order < views.count)
            let totalHeight = CGFloat(views.count - 1)*viewPadding + views.map({$0.bounds.size.height}).reduce(verticalViewOffset, combine: { $0 + $1 })
            let viewsTop = 0.5 * (vcHeight - totalHeight) >= 0 ? 0.5*(vcHeight - totalHeight) : 0
            
            var acc:CGFloat = 0
            for i in 0..<order{
                acc += viewPadding + views[i].bounds.size.height
            }
            return viewsTop + acc
        }
        
    }
    
    
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
